package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.entity.Student;
import com.itheima.entity.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询
     * @param param
     * @return
     */
    @GetMapping("/students")
    public Result page(StudentQueryParam  param){
        PageBean pageBean = studentService.page(param);
        return Result.success(pageBean);
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/students")
    public Result add(@RequestBody Student student){
        studentService.add(student);
        return Result.success();
    }

    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    @GetMapping("/students/{id}")
    public Result getById(@PathVariable Integer id){
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学生
     * @param student
     * @return
     */
    @PutMapping("/students")
    public Result update(@RequestBody Student student){
        studentService.update(student);
        return Result.success();
    }

    /**
     * 批量删除学生
     * @return
     */
    @DeleteMapping("/students/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 添加违纪
     * @param id
     * @param score
     * @return
     */
    @PutMapping("/students/violation/{id}/{score}")
    public Result missing(@PathVariable Integer id, @PathVariable Short score){
        studentService.missScore(id,score);
        return Result.success();
    }
}
